# This code is part of cqlib.
#
# Copyright (C) 2024 China Telecom Quantum Group, QuantumCTek Co., Ltd.,
# Center for Excellence in Quantum Information and Quantum Physics.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
visualization module defines methods to visualize qcis circuits and plotting experiment results.
"""

import logging

from .circuit import draw_text, draw_mpl, TextDrawer, MatplotlibDrawer

_logger = logging.getLogger('cqlib.vis')

try:  # pragma: no cover - depends on external backends
    from .gplot import draw_gplot
except Exception as exc:  # pragma: no cover - depends on external backends
    def draw_gplot(*_args, **_kwargs):
        raise RuntimeError("Graph plotting backend is unavailable.") from exc

    _logger.warning("Graph plotting unavailable: %s", exc, exc_info=False)

try:  # pragma: no cover - depends on external backends
    from .result import draw_histogram
except Exception as exc:  # pragma: no cover - depends on external backends
    def draw_histogram(*_args, **_kwargs):
        raise RuntimeError("Histogram plotting backend is unavailable.") from exc

    _logger.warning("Result plotting unavailable: %s", exc, exc_info=False)

try:  # pragma: no cover - depends on external backends
    from .bloch import (
        plot_bloch_vector,
        plot_bloch_multivector,
        state_to_bloch_vectors,
        statevector_dict_to_array,
    )
except Exception as exc:  # pragma: no cover - depends on external backends
    def plot_bloch_vector(*_args, **_kwargs):
        raise RuntimeError("Bloch plotting backend is unavailable.") from exc

    def plot_bloch_multivector(*_args, **_kwargs):
        raise RuntimeError("Bloch plotting backend is unavailable.") from exc

    def state_to_bloch_vectors(*_args, **_kwargs):
        raise RuntimeError("Bloch plotting backend is unavailable.") from exc

    def statevector_dict_to_array(*_args, **_kwargs):
        raise RuntimeError("Bloch plotting backend is unavailable.") from exc

    _logger.warning("Bloch vector plotting unavailable: %s", exc, exc_info=False)

__all__ = [
    'draw_text', 'draw_mpl', 'TextDrawer', 'MatplotlibDrawer',
    'draw_gplot',
    'draw_histogram',
    'plot_bloch_vector', 'plot_bloch_multivector',
    'state_to_bloch_vectors', 'statevector_dict_to_array',
]
